

'use strict';

const express=require('express');
const path=require('path');
const xtpl=require('xtpl');
const moment=require('moment');
// 验证码模块
const captchapng=require('captchapng');
const md5=require('md5');




const db=require(path.join(__dirname,"../configs/database.js"));
const base=require(path.join(__dirname,"./baseController.js"));

// 来到登入界面
module.exports.signin=function(req,res){

   var data={
       template:path.join(__dirname,"../views/admin/signin.html"),
       data:{},
       res:res

   };
   base.assign(data);

};

module.exports.signinData=function(req,res){

 if(req.session.vcode != req.body.vcode){

        base.showMsg(res,'验证码不正确','/main/signin');
        return false;
    }

    var admin_name = req.body.admin_name;
    var admin_pwd = md5(req.body.admin_pwd);


    if(req.body.admin_type=="1"){

      let where=`admin_name='${admin_name}' AND admin_pwd='${admin_pwd}' AND admin_type='${req.body.admin_type}'`;

      db.table('admin').where(where).find().then(function(data){

          if(JSON.stringify(data) == "{}"){

            base.showMsg(res,'账号密码错误','/main/signin');
            return false;
        }else{

            req.session.admin = data;
            
            if(req.body.auto)
            {
                res.cookie('auto',req.body.auto,{maxAge: 600000 , httpOnly: true, 'signed': false});
                res.cookie('admin_id',data.admin_id,{maxAge: 600000 , httpOnly: true, 'signed': false});
            }

            base.showMsg(res,'登录成功','/stu/stu_list');
            return false;
        }


      }).catch(function(err){

      if(err){

            base.showMsg(res,'账号密码错误','/main/signin');
            return false;
        }


      });
       
    }else if(req.body.admin_type=="2"){


        let where=`admin_name='${admin_name}' AND admin_pwd='${admin_pwd}' AND admin_type='${req.body.admin_type}'`;

      db.table('admin').where(where).find().then(function(data){

          if(JSON.stringify(data) == "{}"){

            base.showMsg(res,'账号密码错误','/main/signin');
            return false;
        }else{
            req.session.admin = data;
            if(req.body.auto)
            {
                res.cookie('auto',req.body.auto,{maxAge: 600000 , httpOnly: true, 'signed': false});
                res.cookie('admin_id',data.admin_id,{maxAge: 600000 , httpOnly: true, 'signed': false});
            }

            base.showMsg(res,'登录成功','/tea/tea_list');
            return false;
        }


      }).catch(function(err){

      if(err){

            base.showMsg(res,'账号密码错误','/main/signin');
            return false;
        }


      });
       



    }else if(req.body.admin_type=="0"){


    let where=`admin_name='${admin_name}' AND admin_pwd='${admin_pwd}' AND admin_type='${req.body.admin_type}'`;

      db.table('admin').where(where).find().then(function(data){

          if(JSON.stringify(data) == "{}"){

            base.showMsg(res,'账号密码错误','/main/signin');
            return false;
        }else{
            req.session.admin = data;
            if(req.body.auto)
            {
                res.cookie('auto',req.body.auto,{maxAge: 600000 , httpOnly: true, 'signed': false});
                res.cookie('admin_id',data.admin_id,{maxAge: 600000 , httpOnly: true, 'signed': false});
            }

            base.showMsg(res,'登录成功','/sys/system');
            return false;
        }


      }).catch(function(err){

      if(err){

            base.showMsg(res,'账号密码错误','/main/signin');
            return false;
        }


      });


    }



//     var where = `admin_name = '${admin_name}' AND admin_pwd = '${admin_pwd}'`;
//     db.table("admin").where(where).find().then(function(data){

//         if(JSON.stringify(data) == "{}")
//         {
//             base.showMsg(res,'账号密码错误','/main/signin');
//             return false;
//         }else{
//             req.session.admin = data;
//             if(req.body.auto)
//             {
//                 res.cookie('auto',req.body.auto,{maxAge: 600000 , httpOnly: true, 'signed': false});
//                 res.cookie('admin_id',data.admin_id,{maxAge: 600000 , httpOnly: true, 'signed': false});
//             }

//             base.showMsg(res,'登录成功','/class/class_list');
//             return false;
//         }




//     }).catch(function(err){
//         if(err)
//         {
//             base.showMsg(res,'账号密码错误','/main/signin');
//             return false;
//         }
//     });
};








module.exports.getVcodeImage = function(req,res){

    var vcode = parseInt(Math.random()*9000+1000);
    var p = new captchapng(80,30,vcode);
    p.color(0,0,0,0);
    p.color(80,80,80,255);
    req.session.vcode = vcode;

    var img = p.getBase64();
    var imgbase64 = new Buffer(img,'base64');
    res.writeHead(200,{
        'Content-Type':'image/png'
    });
    res.end(imgbase64);
};




module.exports.singout=function(req,res){

  req.session.admin=null;
  res.cookie('auto',undefined);
  res.cookie('admin_id',undefined);
  res.redirect('/main/signin');
  return false;



};


module.exports.signup=function(req,res){

  let data={
      
      template:path.join(__dirname,"../views/admin/signup.html"),
      data:{},
      res:res

  };
  base.assign(data);
  

};

module.exports.signupData=function(req,res){

  let admin_name=req.body.admin_name;
  let where=`admin_name='${admin_name}'`;

  db.table('admin').where(where).find().then(function(data){
       if(JSON.stringify(data)!="{}"){
           base.showMsg(res,'用户名已经存在','/main/signup');
           return false;
       }else{
           let data={
              "admin_name":req.body.admin_name,
              "admin_pwd":md5(req.body.admin_pwd),
           };

           db.table('admin').add(data).then(function(affectRows){
           	  base.showMsg(res,'注册成功请登入','/main/signin');
           }).catch(function(err){
             if(err){
                  base.showMsg(res,'注册失败','/main/signup');
                  return false;
             }

           });
       }
  });
};


